import { Toast } from "vant";
import cTitle from "components/title";
import { scrollMixin } from "../../../utils/mixin";
import sellOrderItem from "../consumptionComm/sellOrderItem/sellOrderItem";

export default {
  components: {
    cTitle,
    sellOrderItem
  },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      query: {
        cstore_id: null,
        coupon_member_kwd: "",
        clerk_member_kwd: "",
        order_sn: "",
        goods_name: "",
        start_time: "",
        end_time: ""
      },
      isLoadMore: true,
      page: 1,
      totalPage: 0,
      examineList: [],
      startShow: false, //开始时间弹窗
      endShow: false, //结束时间弹窗
      currentDate: new Date(),
      terminateDate: new Date(),
      minDate: new Date(),
      startDate: "",
      endDate: "",
      formatter(type, value) {
        if (type === "year") {
          return `${value}年`;
        }
        if (type === "month") {
          return `${value}月`;
        }
        return value;
      }
    };
  },
  activated() {
    if ((this.$route.query.storeId ?? "") !== "") {
      this.query.cstore_id = Number(this.$route.query.storeId);
    }
    let dateTime = new Date().getFullYear(); /* 获取现在的年份 */
    this.minDate = new Date(new Date().setFullYear(dateTime - 10));
    this.examineLists();
  },
  methods: {
    startTimeOn() {
      this.startShow = true;
    },
    endTimeOn() {
      this.endShow = true;
    },
    formatDate(date) {
      date = new Date(date);
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    },
    onSearch() {
      this.examineLists();
    },
    onClose(type) {
      if (type === "start") {
        this.startShow = false;
      } else {
        this.endShow = false;
      }
    },
    onConfirm(value, type, event) {
      let setTime = Math.round(new Date(value).getTime() / 1000);
      let endTime = new Date(new Date(new Date(value).toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1) / 1000;
      if (type === "start") {
        this.startShow = false;
        this.startDate = this.formatDate(value);
        this.query.start_time = setTime;
      } else {
        this.endShow = false;
        this.endDate = this.formatDate(value);
        this.query.end_time = parseInt(endTime);
      }
    },
    examineLists() {
      //核销明细列表
      let json = this.query;
      json.page = this.page;
      $http.post("plugin.coupon-store.frontend.store-manage.examineList", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.goodsMore = true;
            this.totalPage = res.last_page;
            if (!this.totalPage) {
              this.page = 0;
            }
            this.examineList = res.data;
          } else {
            this.page = this.page - 1;
            this.goodsMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.totalPage) {
        return;
      } else {
        this.page += +1;
        let json = this.query;
        json.page = this.page;
        $http.post("plugin.coupon-store.frontend.store-manage.examineList", json, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.goodsMore = true;
              this.examineList = [...this.examineList, ...list];
            } else {
              this.page = this.page - 1;
              this.goodsMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    },
    handleScroll() {
      //滚动事件触发
      if (this.getScrollTop() + this.getClientHeight() + 105 > this.getScrollHeight()) {
        //此处发起请求
        if (this.isLoadMore) {
          this.getMoreData();
        } else {
          console.log("没有更多数据");
        }
      }
    }
  }
};
